package es.andresimon.portfolio.security;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import es.andresimon.portfolio.security.UserRole;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;

@Entity
@Configurable
public class PortfolioUser {

    @NotNull
    @Size(min = 1, max = 100)
    private String name;

    @NotNull
    @Size(min = 1, max = 10)
    private String login;

    @NotNull
    @Size(min = 1, max = 10)
    private String password;

    @NotNull
    @Size(min = 1, max = 100)
    private String email;

    @Enumerated
    private UserRole userRole;

	public static Query findPortfolioUsersByLoginEquals(String login) {
        if (login == null || login.length() == 0) throw new IllegalArgumentException("The login argument is required");
        EntityManager em = PortfolioUser.entityManager();
        Query q = em.createQuery("SELECT PortfolioUser FROM PortfolioUser AS portfoliouser WHERE portfoliouser.login = :login");
        q.setParameter("login", login);
        return q;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	@Transactional(propagation = Propagation.REQUIRES_NEW)
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            PortfolioUser attached = this.entityManager.find(this.getClass(), this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public PortfolioUser merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        PortfolioUser merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public static final EntityManager entityManager() {
        EntityManager em = new PortfolioUser().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	@Transactional
    public static long countPortfolioUsers() {
        return ((Number) entityManager().createQuery("select count(o) from PortfolioUser o").getSingleResult()).longValue();
    }

	@SuppressWarnings("unchecked")
    @Transactional
    public static List<PortfolioUser> findAllPortfolioUsers() {
        return entityManager().createQuery("select o from PortfolioUser o").getResultList();
    }

	@Transactional
    public static PortfolioUser findPortfolioUser(Long id) {
        if (id == null) return null;
        return entityManager().find(PortfolioUser.class, id);
    }

	@SuppressWarnings("unchecked")
    @Transactional
    public static List<PortfolioUser> findPortfolioUserEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("select o from PortfolioUser o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Id: ").append(getId()).append(", ");
        sb.append("Version: ").append(getVersion()).append(", ");
        sb.append("Name: ").append(getName()).append(", ");
        sb.append("Login: ").append(getLogin()).append(", ");
        sb.append("Password: ").append(getPassword()).append(", ");
        sb.append("Email: ").append(getEmail()).append(", ");
        sb.append("UserRole: ").append(getUserRole());
        return sb.toString();
    }

	public String getName() {
        return this.name;
    }

	public void setName(String name) {
        this.name = name;
    }

	public String getLogin() {
        return this.login;
    }

	public void setLogin(String login) {
        this.login = login;
    }

	public String getPassword() {
        return this.password;
    }

	public void setPassword(String password) {
        this.password = password;
    }

	public String getEmail() {
        return this.email;
    }

	public void setEmail(String email) {
        this.email = email;
    }

	public UserRole getUserRole() {
        return this.userRole;
    }

	public void setUserRole(UserRole userRole) {
        this.userRole = userRole;
    }
}
